package com.glodon.paas.account.security.oauth1.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Don Li
 */
public class RememberRequestUrlFilter implements Filter {
    public static final String REMEMBERED_REQUEST_URL = "REMEMBERED_REQUEST_URL";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        final HttpServletRequest httpReq = (HttpServletRequest) request;
        if (httpReq.getQueryString() != null) {
            httpReq.getSession().setAttribute(REMEMBERED_REQUEST_URL, httpReq.getRequestURL().append("?").append(httpReq.getQueryString()).toString());
        } else {
            httpReq.getSession().setAttribute(REMEMBERED_REQUEST_URL, httpReq.getRequestURL().toString());
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
